<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/4/14
 * Time: 2:09 下午
 */

namespace app\api\controller\v25;

use app\api\controller\v13\Auth2;
use app\common\cachemodel\Article;
use app\common\cachemodel\ArticleCommentReply;
use app\common\cachemodel\ArticleRead;
use app\common\cachemodel\GoddessArea;
use app\common\cachemodel\GoddessBaseInfo;
use app\common\lib\Util;
use app\common\model\User;
use think\Env;

/**
 * 互动消息
 * @version 2.6.0
 * Class InteractMsg
 * @package app\api\controller\v25
 */
class Interact extends Auth2
{
    protected $replyIcon;
    protected $areaIcon;
    protected $articleIcon;

    public function _initialize()
    {
        parent::_initialize();
        $oss = Env::get('host.photo');
        $this->replyIcon = $oss . 'app/interact/hdicon_message.png';
        $this->areaIcon = $oss . 'app/interact/hdicon_area.png';
        $this->articleIcon = $oss . 'app/interact/hdicon_article.png';
    }

    public function msg()
    {
        $isYang = $this->user['young_model'] ?? false;// 是否开启了青少年模式
        $replyModel = new ArticleCommentReply();
        $data = [];

        $reply_ids = $this->user ? $replyModel->getUserReplyMeFromCache($this->user['id']) : [];//所有回复id
        $messageLink = Util::CheckAppVersion('2.8.0') === '小' ? 'reply_me?need_auth=' . ($isYang ? 3 : 2) : 'message_center?type=4&need_auth=2';
        if ($reply_ids)
        {
            $read_ids = $replyModel->getUserReadReply($this->user['id']);//获取所有已读的回复id
            //获取最新的回复信息
            $replyInfo = $replyModel->getInfoById(current($reply_ids), ['uid1', 'is_goddess']);
            if ($replyInfo['is_goddess'])
            {
                $replyUserInfo = (new GoddessBaseInfo())->getInfo(['nickname']);
            } else
            {
                $replyUserInfo = current((new User())->batchGetUserInfo([$replyInfo['uid1']], ['nickname']));//获取评论者信息
            }
            $unreadReplyNum = count(array_diff($reply_ids, $read_ids));
            $data[] = [
                'title' => '互动消息',
                'icon' => $this->replyIcon,
                'content' => (is_array($replyUserInfo) ? $replyUserInfo['nickname'] ?? '' : $replyUserInfo ?: '') . '评论了你',
                'tip_type' => $unreadReplyNum ? 1 : 0,
                'unread_num' => $unreadReplyNum,
                'target' => 'local',
                'link' => $messageLink
            ];
        } else
        {
            $data[] = [
                'title' => '互动消息',
                'icon' => $this->replyIcon,
                'content' => '',
                'tip_type' => 1,
                'unread_num' => 0,
                'target' => 'local',
                'link' => $messageLink
            ];
        }

        $areaArticleModel = new GoddessArea();
        $areaInfo = $areaArticleModel->getNewestArea();//获取最新的话题文章信息
        $areaReadIds = $this->user ? (new ArticleRead())->getUserArticleReadIds($this->user['id'], 3) : [];//获取用户所有已读的话题id
        $unreadAreaNum = count(array_diff([$areaInfo['id']], $areaReadIds));
        $data[] = [
            'title' => '准了星话题',
            'icon' => $this->areaIcon,
            'content' => $areaInfo['title'],
            'tip_type' => $unreadAreaNum ? 2 : 0,
            'unread_num' => $unreadAreaNum,
            'target' => 'local',
            'link' => "article_info?id={$areaInfo['id']}&type=3&content_type=1&need_auth=" . ($isYang ? 3 : 0)
        ];

        $articleModel = new Article();
        $articleInfo = $articleModel->getNewestArticle($this->headers);//获取最新的文章信息

        $articleReadIds = $this->user ? (new ArticleRead())->getUserArticleReadIds($this->user['id'], 1) : [];//获取用户所有已读的文章id
        $unreadArticleNum = count(array_diff([$articleInfo['id']], $articleReadIds));
        $data[] = [
            'title' => '互动星文',
            'icon' => $this->articleIcon,
            'content' => $articleInfo['title'],
            'tip_type' => $unreadArticleNum ? 2 : 0,
            'unread_num' => $unreadAreaNum,
            'target' => 'local',
            'link' => "article_info?id={$articleInfo['id']}&type=1&content_type=1&need_auth=" . ($isYang ? 3 : 0)
        ];

        return show(config('code.success'), '请求成功', $data);
    }
}
